<template>
    <div id="role">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">角色设置</el-breadcrumb-item>
            <!--            <el-breadcrumb-item>分成统计</el-breadcrumb-item>-->
        </el-breadcrumb>
        <el-card class="box-card">
            <el-form ref="form" label-width="80px" :inline="true" size="small">
                <el-form-item>
                    <el-button type="primary" size="small" @click="addRecharge">添加</el-button>
                </el-form-item>
            </el-form>
            <el-table
                    :data="tableData"
                    border
                    style="width: 100%"
                    size="small">
                <el-table-column
                        type="index"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="name"
                        :width="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'120':''"
                        label="名称">
                </el-table-column>
                <el-table-column
                        prop="remark"
                        label="说明">
                </el-table-column>
                <el-table-column
                        prop="updated_at"
                        :width="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'150':''"
                        label="日期">
                </el-table-column>
                <el-table-column
                        prop="updated_at"
                        label="操作"
                        width="235">
                    <template slot-scope="scope">
                        <el-button type="primary"
                                   plain size="small"
                                   @click="roleJurisdiction(scope.row.id)"
                                   :disabled="scope.row.id == 1">
                            设置权限
                        </el-button>
                        <el-button type="success" plain size="small" @click="edit(scope.row.id)"
                                   :disabled="scope.row.id == 1">编辑
                        </el-button>
                        <el-button type="danger" plain size="small" @click="remove(scope.row.id)"
                                   :disabled="scope.row.id == 1">删除
                        </el-button>

                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <el-dialog :title="fromTitle" :width="this.$store.state.dialogWidth" :visible.sync="visible">
            <el-row :gutter="20">
                <el-col :span="this.$store.state.elColNum">
                    <el-form ref="form" :label-width="this.$store.state.labelWidth" size="small">
                        <el-form-item label="角色名称">
                            <el-input v-model="form.name" :class="windowWidth>=750?'maxWidth':''"></el-input>
                        </el-form-item>
                        <el-form-item label="备注">
                            <el-input v-model="form.remark" style="width: 100%"></el-input>
                        </el-form-item>
                        <el-form-item label="状态">
                            <el-select v-model="form.status" placeholder="状态" style="width: 100%">
                                <el-option label="禁用" value="0"></el-option>
                                <el-option label="启用" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="visible=false" size="small">取 消</el-button>
                <el-button type="primary" @click="dialogAffirm" size="small">确 定</el-button></span>
        </el-dialog>
    </div>
</template>

<script>
    import {isRealNum} from '../../utils/common'

    export default {
        name: "role",
        data() {
            return {
                tableData: [],
                fromTitle: '',
                visible: false,
                form: {
                    name: '',
                    status: '',
                    remark: ''
                },
                id: '',
                windowWidth: document.documentElement.clientWidth,  //实时屏幕宽度
                maxWidth: '100%',
            }
        },
        created() {
            this.getList()
        },
        methods: {
            getList() {
                let data = {
                    page: 1,
                    page_size: 100,
                };
                this.$axios.post('admin/role/index', data).then((res) => {
                    let data = res.data;
                    this.tableData = data;
                });
            },
            addRecharge() {
                this.fromTitle = '新增';
                this.id = '';
                this.form = {
                    name: '',
                    status: '',
                    remark: ''
                };
                this.visible = true
            },
            dialogAffirm() {
                if (this.id) {
                    //编辑
                    this.$axios.put('admin/role/' + this.id, this.form).then((res) => {
                        this.$message.success('成功');
                        this.getList();
                        this.visible = false;
                    });
                } else {
                    // 添加
                    this.$axios.post('/admin/role', this.form).then((res) => {
                        this.$message.success('成功');
                        this.getList();
                        this.visible = false;
                    });
                }
            },
            edit(id) {
                this.id = id;
                this.fromTitle = '编辑';
                this.$axios.get('admin/role/' + id, {}).then((res) => {
                    let data = res.data;
                    for (let key in data) {
                        if (isRealNum(data[key])) {
                            data[key] = data[key].toString();
                        }
                    }
                    this.form = data;
                    this.visible = true;
                });
            },
            remove(id) {
                this.$confirm('确定要进行操作, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.del('admin/role/' + id, '').then((res) => {
                        this.$message.success('删除成功!');
                        this.getList();
                    });
                }).catch(() => {

                });
            },
            roleJurisdiction(id) {
                this.$router.push({
                    path: '/roleJurisdiction',
                    query: {
                        id: id
                    }
                })
            }
        }
    }
</script>

<style scoped>



</style>